/*
 */

package jas.rpc.server;

import jas.http.logging.DateRequestLineResponseCodeIPHttpLogger;
import jas.http.logging.HttpLogger;
import jas.http.server.ConnectionHandler;
import jas.http.server.HttpServerFactory;
import jas.http.server.ThreadedConnectionHandler;
import jas.rpc.http.RpcHttpResponseFactory;
import jas.rpc.http.RpcHttpServerFormatter;
import jas.rpc.http.RpcHttpServerFormatterImpl;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 *
 */
public class RpcServerFactory implements HttpServerFactory {
    
    private ObjectIdBank ids;
    private RpcListener listener;
    
    public RpcServerFactory(ObjectIdBank ids) {
        this.ids = ids;
        this.listener = new RpcListenerImpl();
    }
    
    public RpcServerFactory(ObjectIdBank ids, RpcListener listener) {
        this.ids = ids;
        this.listener = listener;
    }

    public int getListenPort() {
        return 8080;
    }

    public HttpLogger createLogger() {
        return new DateRequestLineResponseCodeIPHttpLogger(System.out, System.err);
    }

    public ConnectionHandler createConnectionHandler() {
        ObjectServerSerializer serializer = new Base64ServerSerializer();
        RpcHttpServerFormatter formatter = new RpcHttpServerFormatterImpl(serializer, ids);
        RpcHttpResponseFactory factory = new RpcHttpResponseFactory(listener, formatter);
        
        HttpLogger logger = createLogger();
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 6, 200, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        
        return new ThreadedConnectionHandler(factory, logger, threadPool);
    }

}
